
#import "FWRootWindowController.h"
#import "FWBlockedAppListController.h"

@interface FWRootWindowController () <NSWindowDelegate>

@property (nonatomic, strong) FWBlockedAppListController *blockedAppListController;

@end

@implementation FWRootWindowController

#pragma mark -
#pragma mark - Life Cycle

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    //BLAuthentication * BLAuth = [[BLAuthentication alloc] init];
    [self.blockedAppListController setupSavedRecords ];
    NSString *logPath = @"/var/log/firewall.log";
    NSArray * args3 = [[NSArray alloc] initWithObjects:@"644",logPath , nil];
    [[BLAuthentication shared] executeCommand:@"/bin/chmod" withArgs:args3];
    for(int i =0;i<50000000;i++);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Awake" object:nil];
   //  [[NSNotificationCenter defaultCenter] postNotificationName:@"Load" object:nil];
    
    [self setupBlockedAppListController];
    
    self.window.delegate = self;
}
-(void)awakeFromNib
{

    NSString *p=[[NSBundle mainBundle]resourcePath];
    
    NSString * pathToKext = [p stringByAppendingString:@"/ApplicationFireWall.kext"];//путь к кексту
    //смена прав доступа к кексту
    NSArray * args;
    args = [NSArray arrayWithObjects: @"-R", @"root:wheel", pathToKext, nil];
    [[BLAuthentication shared] executeCommand:@"/usr/sbin/chown" withArgs: args];
    
    //загрузка драйвера
    NSArray * args2;
    args2 = [NSArray arrayWithObjects: pathToKext, nil];
    [[BLAuthentication shared] executeCommand:@"/sbin/kextload" withArgs: args2];
}

- (void)windowWillClose:(NSNotification *)notification
{
    
    // BLAuthentication * BLAuth = [[BLAuthentication alloc] init];
     [[NSNotificationCenter defaultCenter] postNotificationName:@"Save" object:nil];
    NSString *p=[[NSBundle mainBundle]resourcePath];
    
    NSString * pathToKext = [p stringByAppendingString:@"/ApplicationFireWall.kext"];//путь к кексту
    
    NSArray * args4;
    args4 = [NSArray arrayWithObjects: pathToKext, nil];
    [[BLAuthentication shared] executeCommand:@"/sbin/kextunload" withArgs: args4];

}


- (void)setupBlockedAppListController
{
    if (!self.blockedAppListController) {
        self.blockedAppListController = [FWBlockedAppListController new];
    }
}

@end
